package com.spc.car.web.log.module;

import com.spc.car.common.utils.StringUtils;
import com.spc.car.common.utils.spring.SpringUtils;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.domain.project.po.ProjectTeamPO;
import com.spc.car.data.entity.log.LogProjectOperation;
import com.spc.car.data.entity.project.ProjectTeam;
import com.spc.car.data.service.project.ProjectTeamService;
import com.spc.car.web.log.ProjectAction;
import com.spc.car.web.log.annotation.LogProjectAction;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class LogProjectTeam extends BaseLogModule<LogProjectOperation, LogProjectAction> {
    public ProjectTeamService teamService;

    public LogProjectTeam() {
        this.teamService = SpringUtils.getBean(ProjectTeamService.class);
    }

    public LogProjectOperation toLogRecord(LogProjectAction logProjectAction,
                                           Map<String, Object> paramsMap,
                                           AjaxResult result) {
        LogProjectOperation logProjectOperation = new LogProjectOperation();

        ProjectAction[] projectActions = logProjectAction.action();
        Object idObj = paramsMap.get(logProjectAction.idForPost());
        ProjectTeam projectTeam = null;
        String projectTenderingCode = null;
        if (Objects.nonNull(idObj) && idObj instanceof String) {
            projectTenderingCode = (String)idObj;
        } else if (Objects.nonNull(idObj) && idObj instanceof Long) {
            projectTeam = teamService.getById((Long)idObj);
        }

        for (ProjectAction projectAction: projectActions) {
            if (projectAction.equals(ProjectAction.ADD_MEMBER)
                    && StringUtils.isNotBlank(projectTenderingCode)) {
                logProjectOperation.setProjectTenderingCode(projectTenderingCode);
                Object objList = paramsMap.get("memberList");
                String userNames = "没有成员";
                if (Objects.nonNull(objList)) {
                    List<ProjectTeamPO> projectTeamPOList = (List)objList;
                    if (CollectionUtils.isNotEmpty(projectTeamPOList)) {
                        userNames = StringUtils.join(projectTeamPOList.stream()
                                .map(ProjectTeamPO::getUserName).collect(Collectors.toList()),
                                ",");
                    }
                }
                logProjectOperation.setRemark(projectAction.getName()
                            + "[" + userNames + "]");
                break;
            } else if (projectAction.equals(ProjectAction.UPDATE_MEMBER)
                    && Objects.nonNull(projectTeam)) {
                logProjectOperation.setProjectTenderingCode(projectTeam.getProjectTenderingCode());
                logProjectOperation.setRemark(projectAction.getName()
                        + "[" + projectTeam.getUserName() + "]");
                break;
            } else if (projectAction.equals(ProjectAction.REMOVE_MEMBER)
                    && Objects.nonNull(projectTeam)) {
                logProjectOperation.setProjectTenderingCode(projectTeam.getProjectTenderingCode());
                logProjectOperation.setRemark(projectAction.getName()
                        + "[" + projectTeam.getUserName() + "]");
                break;
            }
        }

        return logProjectOperation;
    }
}
